/**
 * 公用的品牌判断，经销商类型等
 *
 * @Author: baiyuxin
 * @Date: 2019-9-23
 * @Last Modified by:   baiyuxin
 * @Last Modified time: 2019-9-23 14:35:22
 */

import { getAppId } from '@/assets/js/auth';//获取登录用户
import { dictionary } from '@/utils/dictMixin';

import * as originInfoTypes from '@/api/cms-api/origin/originInfo/types';//经销商信息维护的字典
import * as constructTypes from '@/api/cms-api/origin/construct/dealer/types';
import { region } from '@/api/cms-common/region';//行政区域
import { getRegion } from '@/api/cms-common/dealerdata/index';//事业部，办事处

/**
 * arcfox品牌常量
 */
export const BRAND_ARCFOX = 'arcfox';   //ARCFOX品牌

/**
 * 字典值
 */
export let DICT_CODE = 2538;

/**
 * 获取AppId(可能存在退出换品牌，品牌不变的情况)
 */
export const appId = getAppId();

// export const BIGCUSTOMER_ORG_NAME = '北京事业部';
export const BIGCUSTOMER_ORG_NAME = '大客户直销';

/**
 * 判断当前登录人的appId是Arcfox吗？
 */
export const isArcfox = () =>{
	let _appId = getAppId().toLocaleLowerCase();
	if(BRAND_ARCFOX.toLocaleLowerCase() === _appId){
		return true;
	}
	return false;
}

/**
 * 根据excludeList排除,disableList禁用
 * @param {*} sourceList 源list，必填，
 * @param {*} excludeList 排除这些,可以是字符，可以是数字
 * @param {*} disableList 禁用这些,可以是字符，可以是数字
 * @param {*} isDisabled disableList是否禁用，true：禁用，false：启用
 */
export const cleanUnnecessary = (sourceList,excludeList,disableList,isDisabled = true) => {
	//都为空直接返回源数据
	if(!Array.isArray(excludeList) && !Array.isArray(disableList)){
	//如果数据异常，返回空
		if(Array.isArray(sourceList)){
			return sourceList;
		}else{
			return [];
		}
	}
	//如果数据异常，返回空
	if(!sourceList){
		return [];
	}else{
		//{ text: x.codeCnDesc, value: x.codeId }
		let outList = [],sourceMap = {},excludeMap = {},disableMap = {};
		sourceList.map((x,index) => {
			x.__order = index;
			//如果选择禁用disableList,则先禁用所有
			// if(!isDisabled){
				x.disabled = !isDisabled;
			// }
			sourceMap[x.value] = x;
		});
		if(Array.isArray(excludeList)){
			excludeList.map((x) => {excludeMap[x] = x});
		}
		if(Array.isArray(disableList)){
			disableList.map((x) => {disableMap[x] = x});
		}
		//删除需要排除的
		for(let m in excludeMap){
			delete sourceMap[m];
		}
		// console.log("sourceMap =>",JSON.stringify(sourceMap));
		//禁用需要禁用的（isDisabled在等于false时解开禁用）
		for(let m in disableMap){
			// console.log("disableMap[",m,"]=>",isDisabled);
			// sourceMap[m].disabled = isDisabled;
		}
		// console.log("sourceMap =>",JSON.stringify(sourceMap));
		//生成最后返回的list
		for(let m in sourceMap){
			outList.push(sourceMap[m]);
		}
		outList.sort((a,b) => {
			return a.__order - b.__order;
		});

		return outList;
	}
}

export const dealerCommon = {
	mixins: [region],
	mounted(){
		//console.log(this.createDictList(DICT_CODE));
	},
	methods: {
		isArcfox:isArcfox,
		cleanUnnecessary:cleanUnnecessary,
		/**
		 * 根据默认Code查询，根据当前品牌去除【SPACE，RACE】项（arcfox需要这两项）
		 */
		getOriginInfoDealerType(){
			let dictList = Object.assign([],this.createDictList(DICT_CODE));
			let excludeList = [originInfoTypes.DEALER_INFO_DEALER_TYPE_DIRECT_SHOP,originInfoTypes.DEALER_INFO_DEALER_TYPE_NO_DIRECT_SHOP];
			if(isArcfox()){
				excludeList = null;
			}
			return this.cleanUnnecessary(dictList,excludeList);
		},
		/**
		 * 根据默认Code查询，根据当前品牌去除【SPACE，RACE】项（arcfox需要这两项）,没有大客户
		 */
		getOriginInfoDealerTypeNoBig(){
			let dictList = Object.assign([],this.createDictList(DICT_CODE));
			let excludeList = [originInfoTypes.DEALER_INFO_DEALER_TYPE_DIRECT_SHOP,originInfoTypes.DEALER_INFO_DEALER_TYPE_NO_DIRECT_SHOP,originInfoTypes.DEALER_INFO_DEALER_TYPE_BIG_CUSTOMER];
			if(isArcfox()){
				excludeList = [originInfoTypes.DEALER_INFO_DEALER_TYPE_BIG_CUSTOMER];
			}
			return this.cleanUnnecessary(dictList,excludeList);
		},
		/**
		 * 根据默认Code 查询，根据当前品牌去除【SPACE，RACE】项（arcfox需要这两项）,除了【大客户】其他全禁用了
		 */
		getOriginInfoDealerTypeToNotDisabled(){
			let dictList = Object.assign([],this.createDictList(DICT_CODE));
			let excludeList = [originInfoTypes.DEALER_INFO_DEALER_TYPE_DIRECT_SHOP,originInfoTypes.DEALER_INFO_DEALER_TYPE_NO_DIRECT_SHOP];
			if(isArcfox()){
				excludeList = null;
			}
			return this.cleanUnnecessary(dictList,excludeList,[originInfoTypes.DEALER_INFO_DEALER_TYPE_BIG_CUSTOMER],false);
		},
		/**
		 * 查询事业部（返回标准的事业部数据）
		 */
		async __getDeptData() {
			const res = await getRegion();
			if (res.resultCode === 200) {
				return res.data.map(x => ({value: parseInt(x.id), text: x.orgName }));
			}
			return [];
		},
		/**
		 * 级联查询该事业部下的办事处（返回标准的办事处数据）
		 */
		// async getOfficeDateByDepId (deptId = -99) {
		// 	if(deptId == -99){
		// 		return [];
		// 	}
		// 	const res = await getCommunity(deptId);
		// 	if (res.resultCode === 200) {
		// 		return res.data.map(x => ({ value: parseInt(x.id), text: x.orgName }));
		// 	}
		// 	return [];
		// },
		async _$getBigcustomerData() {
			const dictList = await this.__getDeptData();
			if(Array.isArray(dictList)){
				if(dictList.length > 0){
					//手动过滤其名称（只能根据名称去过滤了，，所以用常量就完事了）
					return dictList.filter(x => x.text === BIGCUSTOMER_ORG_NAME);
				}
			}
			return [];
		},
		async _$getNotBigcustomerData() {
			const dictList = await this.__getDeptData();
			if(Array.isArray(dictList)){
				if(dictList.length > 0){
					//手动过滤其名称（只能根据名称去过滤了，，所以用常量就完事了）
					return dictList.filter(x => x.text != BIGCUSTOMER_ORG_NAME);
				}
			}
			return [];
		},
	// getConstructDealerType(type){
		// 	let dictList = Object.assign([],this.createDictList(type));
		// 	return this.cleanUnnecessary(dictList,[originInfoTypes.DEALER_INFO_DEALER_TYPE_DIRECT_SHOP,originInfoTypes.DEALER_INFO_DEALER_TYPE_NO_DIRECT_SHOP]);
		// },
	}
};
